package com.sx.gateway.conf;

import com.sx.gateway.filter.IPRegisterGatewayFilter;
import com.sx.gateway.filter.TokenGlobalFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.GatewayFilterSpec;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.route.builder.UriSpec;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Function;

/**
 * @author shengx
 * @date 2020/5/16 20:33
 */
@Configuration
@RefreshScope
public class FilterConfiguration {
    @Value("${test}")
    private String test;
    // 令牌桶最大容量
    @Value("${bucket.capacity}")
    private int capacity = 5;
    // 每次Token补充量
    @Value("${bucket.refillTokens}")
    private int refillTokens = 5;
    // 补充时间间隔
    @Value("${bucket.duration}")
    private static int duration = 1;

    @Bean
    public TokenGlobalFilter getTokenFilter(){
        return new TokenGlobalFilter();
    }

//    @Bean
//    public RouteLocator getRouteLocator(RouteLocatorBuilder builder){
//        IPRegisterGatewayFilter ipRegisterGatewayFilter = new IPRegisterGatewayFilter();
//        ipRegisterGatewayFilter.setTest(test);
//        ipRegisterGatewayFilter.setCapacity(capacity);
//        ipRegisterGatewayFilter.setDuration(duration);
//        ipRegisterGatewayFilter.setRefillTokens(refillTokens);
//        return builder.routes().
//                route(new Function<PredicateSpec, Route.AsyncBuilder>() {
//                    @Override
//                    public Route.AsyncBuilder apply(PredicateSpec predicateSpec) {
//                        return predicateSpec
//                                .path("/api/user/**")
//                                .filters(new Function<GatewayFilterSpec, UriSpec>() {
//                                    @Override
//                                    public UriSpec apply(GatewayFilterSpec gatewayFilterSpec) {
//                                        return gatewayFilterSpec.stripPrefix(1).filter(ipRegisterGatewayFilter);
//                                    }
//                                })
//                                .uri("lb://SERVICE-USER")
//                                .id("service-user");
//                    }
//                }).build();
//    }
}
